package com.candy.control;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.candy.service.LoginService;

public class LoginServlet extends HttpServlet{
//	处理get请求的方法

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
//处理post请求的
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		解决中文乱码问题
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		//		接收前段传递进来的参数
		String username=req.getParameter("username");
		String userpwd=req.getParameter("userpwd");
//		调用逻辑层，匹配用户名密码
		LoginService service=new LoginService();
		boolean ok=service.login(username, userpwd);
//		将数据传送给前端
//		将用户信息存放在session中
		req.getSession().setAttribute("username", username);
		
		if(ok) {
//		若用户名密码匹配，转发到登录页面
			req.getRequestDispatcher("layout.html").forward(req, resp);
		}else {
			req.getRequestDispatcher("index.jsp").forward(req, resp);
		}
		
		
	}
	
	
	

}
